// 分类
(function() {
	var db = require("../db/index");
	var config = require("../_include/config");
	var db_table_name = config.dbname.classify;
	var db_id_prefix = config.dbIdPrefix.classify;
	var helper = require("../_include/helper");

	//构造函数
	var _con = function(info /*classify_name, bus_id, parent_classify_id*/ ) {
		var self = this;
		//ID编号
		self._id = db_id_prefix + Math.random().toString(16).substr(2) + (+new Date).toString(16);

		this.setInfo(info);
		//对应的商家
		self.bus_id = info.bus_id;
		//父级分类ID
		self.parent_classify_id = null;
		//包含的商品
		self.content = [];
		//子文件夹
		self.childClassifys = [];

		if (info.parent_classify_id) {
			var parent_classify = this.setParentClassify(info.parent_classify_id);
		}

		self.create_time = +new Date;

		//默认不启用优惠
		// self.enable_buff = false;
		// self.buff_id = null;
		//如果父级有启用优惠的话，默认强制启动优惠，info.type如果为空，那么就继承式
		if (parent_classify && parent_classify.enable_buff) {
			this._enableBuff()
		}

		db.insert(db_table_name, self);
		console.log("创建了一个分类：", info.classify_name);
	};
	//PS:模块的暴露放在require skill前面，因为二者互相依赖
	module.exports = _con;
	//技能
	_con.prototype = require("./skill");

	_con.TYPE = {
		无: 0, //同null，如果启用BUFF，但无type，代表继承
		打折: 1,
		满减: 2,
		现金抵用: 3
	};

	//静态方法
	_con.get = function(Id) {
		var result = db.find_by_id(db_table_name, Id);
		if (result) {
			result.__proto__ = _con.prototype;
		}
		return result;
	};
	var BusCon = require("../bus/_con");
}());